<?php
const APP_ID = 'your_app_id_here';
const APP_SECRET = 'your_app_secret_here';
const URL_PREFIX = 'http://localhost';

//define( 'SELF_NAME', end(explode('/',$_SERVER['PHP_SELF'])) );
define( 'SELF_NAME', basename($_SERVER['PHP_SELF']) );

if(isset($_REQUEST['do']) && $_REQUEST['do']=='make_ts') {
	$ts = time();
	echo(''.$ts);
	exit();
}
if(isset($_REQUEST['do']) && $_REQUEST['do']=='make') {
	$app_id = isset($_REQUEST['app_id']) ? $_REQUEST['app_id'] : '';
	$app_secret = isset($_REQUEST['app_secret']) ? $_REQUEST['app_secret'] : '';
	$ts = isset($_REQUEST['ts']) ? $_REQUEST['ts'] : '';
	$jsonstr = isset($_REQUEST['jsonstr']) ? $_REQUEST['jsonstr'] : '';
	$url_prefix = isset($_REQUEST['url_prefix']) ? $_REQUEST['url_prefix'] : '';
	//...
	$encrypt = build_enrypt($jsonstr, $app_id, $app_secret);
	$ud = array(
			'appid'=>$app_id,
			'ts'=>$ts,
			'encrypt'=>$encrypt,
	);
	//$fs = '?'. build_url_params($ud, 0);
	$fs = '/'. build_url_params($ud, 1);
	echo $url_prefix.$fs;
	exit();
}
function build_url_params($data, $type = 0) {
	$type = (int) $type;
	//URL path模式
	if($type==1) {
		$str = '';
		foreach($data as $k=>$v) {
			$str .= $k.'/'.$v.'/';
		}
		$str = rtrim($str,'/');
	}
	//URL 普通模式
	else {
		$str = http_build_query($data);
	}
	return $str;
}
/*
API接口需以上GET参数，访问形式：appid={$appid}&ts={时间戳}&encrypt={加密密文}
API接口其余参数统一由POST方式提交，提交内容为JSON串。
$app_id, $app_secrect 为约定key和秘钥.
jsonstr为json参数，转为数组取其中维度为一维的参数, 值为空的参数也过滤掉，得到的值再按根据key排序，最后拼接成URL形式字符串，
ts值为GET参数中的ts时间戳，得到encrypt值等于：md5($app_secrect.md5($jsonstr.$app_id))；
*/
function build_enrypt($jsonstr, $app_id='', $app_secret = '') {
	$app_id = $app_id ? $app_id : APP_ID;
	$app_secret = $app_secret ? $app_secret : APP_SECRET;
	$str = '';
	$jsondata = json_decode($jsonstr, true);
	if(is_array($jsondata)) {
		foreach($jsondata as $k=>$val) {
			if(is_array($val) || (empty($val) && strval($val)!=='0') ) {
				unset($jsondata[$k]);
			}
		}
		ksort($jsondata);
		foreach ($jsondata as $k => $v) {
			$str .= $k . '=' . $v . '&';
		}
		$str = rtrim($str, '&');
	}
	//...
	$jsonstr = $str;
	//echo md5($jsonstr.$app_id)."\n";
	$encrypt = md5($app_secret.md5($jsonstr.$app_id));
	return $encrypt;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<base href="./" />
	<meta http-equiv="content-type" content="text/html; charset=utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
	<title>签名算法生成工具</title>
	<script src="statics/Js/jquery.min.js"></script>
	<link rel="stylesheet" type="text/css" href="statics/Css/api-index.css" />
</head>
<body>
<style type="text/css">
	.input-text { width:500px; }
	.input-text2 { width:200px; }
	h3{ font-size:12px; }
	h2{ font-size:14px; }
</style>

<body style="margin-left:3px;">


<br><hr width="600" align=left><!----------------------------->

<div class="main" style="">
	<h3>签名算法生成</h3>
	<div class="block-msg">
		<form method="post" >
			<table width="100%">
				<tr>
					<td align="right" width="100" height="35">app_id：</td>
					<td><input type="text" id="app_id" name="app_id" value="<?echo APP_ID;?>" class="input-text" /></td>
				</tr>

				<tr><td align="right" width="100" height="35">app_secret：</td><td>
						<input type="text" name="app_secret" id="app_secret" value="<?echo APP_SECRET;?>" class="input-text" ></td></tr>

				<tr>
					<td align="right" width="100" height="35">json字符串：</td>
					<td><textarea cols=80 rows=9 id="jsonstr" name="jsonstr"></textarea></td>
				</tr>

				<tr>
					<td align="right" height="35">ts：</td>
					<td><input type="text" id="ts" name="ts" value="<?php echo time();?>" size=15 class="input-text2" /><font color=red>(时间戳)</font> <input type="button" class="button" value="刷新" onclick="makeTs(this)" /></td>
				</tr>

				<tr><td align="right" width="100" height="35">URL前缀：</td><td>
						<input type="text" name="url_prefix" id="url_prefix" value="<?echo URL_PREFIX;?>" class="input-text" ></td></tr>

				<tr>
					<td  align="right" height="35"> </td>
					<td><input type="button" class="button" value="生成数据" onclick="makeDo(this)" /></td>
				</tr>
			</table>

			<table width="100%" style="display:none" id="j_TestArea">
				<tr>
					<td>
						<div style="margin-left:20px;">
							<textarea cols=80 rows=4 name="txt_TestArea" id="txt_TestArea"></textarea>
						</div>
					</td>
				</tr>
			</table>

		</form>
	</div>
</div>
<script type="text/javascript">
	//生成JSON:
	function makeDo(e) {
		var form = $(e).parents('form');
		var data = {
			app_id: $('#app_id',form).val(),
			app_secret: $('#app_secret',form).val(),
			jsonstr: $('#jsonstr',form).val(),
			ts: $('#ts',form).val(),
			url_prefix: $('#url_prefix',form).val(),
		};
		makeDo_Common(form, data);
	}

	//生成JSON:
	function makeDo_Common(form, data) {
		$.ajax( {
			url:"<?php echo SELF_NAME;?>?do=make",// 发送 action
			data:data,
			type:'post',
			cache:false,
			dataType:'text',
			success:function(data) {
				$('#txt_TestArea',form).val(data);
			},
			error : function() {
				//alert("失败！");
			}
		});
		$('#j_TestArea',form).show();
	}

	//生成JSON:
	function makeTs(form) {
		$.ajax( {
			url:"<?php echo SELF_NAME;?>?do=make_ts",// 发送 action
			data:null,
			type:'post',
			cache:false,
			dataType:'text',
			success:function(data) {
				$('#ts').val(data);
			},
			error : function() {
				//alert("失败！");
			}
		});
	}
</script>

<br><hr width="600" align=left><!----------------------------->

<br /><br /><br /><br /><br /><br />
</body>
</html>
